package com.elontian.View.adaper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.elontian.util.CheckUtil;

public abstract class ELTPagerAdapter extends PagerAdapter {

	private List data = new ArrayList();
	private HashMap<Integer, View> cacheMap = new HashMap<Integer, View>();

	// abstract

	public abstract View initItemView(View contentView, int arg1, Object t);

	public ELTPagerAdapter(ArrayList data) {
		// TODO Auto-generated constructor stub
		if (CheckUtil.isNotNullList(data)) {
			this.data = data;
		}

	}

	public void addData(ArrayList data) {
		if (CheckUtil.isNotNullList(data)) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	public void reloadData(ArrayList data) {
		// TODO Auto-generated method stub
		if (CheckUtil.isNotNullList(data)) {
			this.data.clear();
			this.data = data;
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return super.getItemPosition(object);
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		// TODO Auto-generated method stub
		((ViewPager) arg0).removeView(cacheMap.get(arg1));
	}

	@Override
	public Object instantiateItem(View arg0, int arg1) {
		// TODO Auto-generated method stub
		View view = this.initItemView(cacheMap.get(arg1), arg1, data.get(arg1));
		cacheMap.put(arg1, view);
		((ViewPager) arg0).addView(view);
		return view;
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub

	}

}
